package com.cqrcb.cloud.utils;

import org.joda.time.DateTime;

import java.util.Date;

/**
 * @author ：29520
 */
public class DateUtils {
    /**
     * 获取月份的第一天
     *
     * @return {@link Date}
     */
    public static Date getMonthFirstDate(){
        return getMonthFirstDate(new Date());
    }
    public static Date getMonthFirstDate(Date date){
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfMonth().withMinimumValue().toDate();
    }

    /**
     * 获取月份的最后一天
     *
     * @return {@link Date}
     */
    public static Date getMonthLastDate(){
        return getMonthLastDate(new Date());
    }
    public static Date getMonthLastDate(Date date){
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfMonth().withMaximumValue().toDate();
    }

    /**
     * 得到一周的第一天
     *
     * @param date 日期
     * @return {@link Date}
     */
    public static Date getWeekFirstDate(Date date){
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfWeek().withMinimumValue().toDate();
    }

    public static Date getWeekFirstDate(){
        return getWeekFirstDate(new Date());
    }

    /**
     * 得到一周的最后一天
     *
     * @param date 日期
     * @return {@link Date}
     */
    public static Date getWeekLastDate(Date date){
        DateTime dateTime = new DateTime(date);
        return dateTime.dayOfWeek().withMaximumValue().toDate();
    }

    /**
     * 得到一周的第一天
     *
     * @param date 日期
     * @return {@link Date}
     */
    public static Date getWeekFirstDateBeginCompare(Date date,Date beginDate){
        Date weekFirstDate = getWeekFirstDate(date);
        if(new DateTime(beginDate).isAfter(new DateTime(weekFirstDate))){
            weekFirstDate = beginDate;
        }
        return new DateTime(weekFirstDate).millisOfDay().withMinimumValue().toDate();
    }
    public static Date getWeekFirstDateBeginCompare(Date beginDate){
        return getWeekFirstDateBeginCompare(new Date(),beginDate);
    }

    /**
     * 得到一周的最后一天
     *
     * @param date 日期
     * @return {@link Date}
     */
    public static Date getWeekLastDateEndCompare(Date date,Date lastDate){
        Date weekLastDate = getWeekLastDate(date);
        if(new DateTime(weekLastDate).isAfter(new DateTime(lastDate))){
            weekLastDate = lastDate;
        }
        return new DateTime(weekLastDate).millisOfDay().withMaximumValue().toDate();
    }
    public static Date getWeekLastDateEndCompare(Date lastDate){
        return getWeekLastDateEndCompare(new Date(),lastDate);
    }
    public static Date getWeekLastDateEndCompare(int num,Date lastDate){
        return getWeekLastDateEndCompare(DateTime.now().plusWeeks(num - 1).toDate(),lastDate);
    }

    /**
     * 得到一月的第一天
     *
     * @param date 日期
     * @return {@link Date}
     */
    public static Date getMonthFirstDateBeginCompare(Date date,Date beginDate){
        Date monthFirstDate = getMonthFirstDate(date);
        if(new DateTime(beginDate).isAfter(new DateTime(monthFirstDate))){
            monthFirstDate = beginDate;
        }
        return new DateTime(monthFirstDate).millisOfDay().withMinimumValue().toDate();
    }
    public static Date getMonthFirstDateBeginCompare(Date beginDate){
        return getMonthFirstDateBeginCompare(new Date(),beginDate);
    }


    /**
     * 得到一月的最后一天
     *
     * @param date 日期
     * @return {@link Date}
     */
    public static Date getMonthLastDateEndCompare(Date date,Date lastDate){
        Date monthLastDate = getMonthLastDate(date);
        if(new DateTime(monthLastDate).isAfter(new DateTime(lastDate))){
            monthLastDate = lastDate;
        }
        return new DateTime(monthLastDate).millisOfDay().withMaximumValue().toDate();
    }
    public static Date getMonthLastDateEndCompare(Date lastDate){
        return getMonthLastDateEndCompare(new Date(),lastDate);
    }
    public static Date getMonthLastDateEndCompare(int num,Date lastDate){
        return getMonthLastDateEndCompare(DateTime.now().plusMonths(num - 1).toDate(),lastDate);
    }


    /**
     * 得到一天的开始时间
     *
     * @return {@link Date}
     */
    public static Date getDayFirstDateBegin(){
        return getDayFirstDateBegin(new Date());
    }
    public static Date getDayFirstDateBegin(Date date){
        return new DateTime(date).millisOfDay().withMinimumValue().toDate();
    }



    /**
     * 得到一天的结束时间
     *
     * @return {@link Date}
     */
    public static Date getDayLastDateEnd(){
        return getDayLastDateEnd(new Date());
    }
    public static Date getDayLastDateEnd(Date date){
        return new DateTime(date).millisOfDay().withMaximumValue().toDate();
    }
    public static Date getDayLastDateEnd(int num){
        return getDayLastDateEnd(DateTime.now().plusDays(num - 1).toDate());
    }
    public static Date getDayLastDateEndCompare(Date date,Date lastDate){
        if(new DateTime(date).isAfter(new DateTime(lastDate))){
            date = lastDate;
        }
        return new DateTime(date).millisOfDay().withMaximumValue().toDate();
    }
    public static Date getDayLastDateEndCompare(int num,Date lastDate){
        return getDayLastDateEndCompare(DateTime.now().plusDays(num - 1).toDate(),lastDate);
    }

    public static Date getNow() {
        return new Date();
    }


    public static boolean dateEquals(Date date1,Date date2){
        if(date1 == null && date2 == null){
            return true;
        }
        if(date1 != null && date2 != null && date1.getTime() == date2.getTime()){
            return true;
        }
        return false;
    }

    /**
     * 判断时间是否在指定的区间
     * @param begin
     * @param end
     * @param target
     * @return boolean
     */
    public static boolean dateIntervalEquals(Date begin,Date end,Date target){
        if(begin == null || end == null || target == null){
            return false;
        }
        long time = target.getTime();
        if(time >= begin.getTime() && time <= end.getTime()){
            return true;
        }
        return false;
    }

    /**
     * 判断当前日期是否在指定的日期之间
     * @param begin
     * @param end
     * @return boolean
     */
    public static boolean dateIntervalEquals(Date begin,Date end){
        Date target = new Date();
        if(begin == null || end == null || target == null){
            return false;
        }
        long time = target.getTime();
        if(time >= begin.getTime() && time <= end.getTime()){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
//        DateTime.now();
//        System.out.println(new DateTime(getWeekFirstDateBeginCompare(new Date(),new Date())).toLocalDateTime());
//        System.out.println(new DateTime(getWeekLastDateEndCompare(new Date(),new Date())).toLocalDateTime());
//
//        System.out.println(new DateTime(getMonthFirstDateBeginCompare(new Date(),new Date())).toLocalDateTime());
//        System.out.println(new DateTime(getMonthLastDateEndCompare(new Date(),new Date())).toLocalDateTime());
//
//        System.out.println(new DateTime(getDayFirstDateBegin(new Date())).toLocalDateTime());
//        System.out.println(new DateTime(getDayLastDateEnd(new Date())).toLocalDateTime());

        Date weekLastDateEndCompare = getWeekLastDateEndCompare(1, new Date());
        System.out.println(new DateTime(weekLastDateEndCompare).toLocalDateTime());
        System.out.println(DateTime.now().plusWeeks(1).toLocalDateTime());
    }
}
